// pages/apply/apply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    time: '9:00',
    name:"",
    phone: "",
    orderList:[],
    checkedList:[]
  },
  checkboxChange: function (e) {
    var checkedList =[];
    this.data.orderList.forEach(function(v,i){
      e.detail.value.forEach(function(v1,i1){
        if (v.Id == v1){
          checkedList.push(v);
        }
      })
    });
    this.setData({
      checkedList: checkedList
    });
    console.log(checkedList);
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  formSubmit:function(e){
    var openid = app.globalData.openid;
    console.log(openid);
    if (openid){
      if (e.detail.value.name != "" &&  e.detail.value.phone != ""){
        var that = this;
        var data = JSON.parse(JSON.stringify(e.detail.value));
        data.gender =  app.globalData.userInfo.gender;
        data.time = that.data.date +" "+ that.data.time;
        data.goods = JSON.stringify(this.data.checkedList);
        data.openid = openid;
        console.log(data);
        wx.request({
          url: app.globalData.apiUrl + '/apply/add',
          data: data,
          method:"post",
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            if(res){
              that.setData({
                name: "",
                phone: ""
              })
              wx.showToast({
                title: '成功',
                icon: 'success',
                duration: 2000
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: '请填写完整',
          icon: 'success',
          duration: 1000
        })
      }
    }else{
      wx.showToast({
        title: '登录才可预约',
        icon: 'success',
        duration: 1000
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // wx.request({
    //   url: app.globalData.apiUrl + '/major', 
    //   data: {},
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   success: function (res) {
        
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    try {
      var orderList = wx.getStorageSync('orderList')
      this.setData({
        orderList: orderList
      });
      console.log(orderList);
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})